package com.uu.safeguard.utils;


import com.uu.safeguard.common.Constant;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class WsDateUtil {

    static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    /**
     * HH:mm:ss
     * @return
     */
    public static String getHMS(Long time) {
        if(time == null){
            return null;
        }else{
            Date date = new Date(time);
            long hour = time/(60*60*1000);
            long minute = (time - hour*60*60*1000)/(60*1000);
            long second = (time - hour*60*60*1000 - minute*60*1000)/1000;
            return (hour==0?"00":(hour>=10?hour:("0"+hour)))+":"+(minute==0?"00":(minute>=10?minute:("0"+minute)))+":"+(second==0?"00":(second>=10?second:("0"+second)));
        }
    }
    /**
     * 获取指定时间戳所在月份开始的时间戳/秒
     * @return
     */
    public static Long getTodayBegin() {
        Calendar calendar = Calendar.getInstance();     //当前时间
        calendar.add(Calendar.DAY_OF_YEAR,0);
        //将小时至0
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        //将分钟至0
        calendar.set(Calendar.MINUTE, 0);
        //将秒至0
        calendar.set(Calendar.SECOND,0);
        //将毫秒至0
        calendar.set(Calendar.MILLISECOND, 0);
        // 获取本月第一天的时间戳
        //return c.getTimeInMillis() / 1000;
        return calendar.getTimeInMillis();
    }
    /**
     * 获取指定时间戳昨天开始的时间戳/秒
     * @return
     */
    public static Long getYesterdayBegin() {
        Calendar calendar = Calendar.getInstance();     //当前时间
        calendar.add(Calendar.DAY_OF_YEAR,-1);
        //将小时至0
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        //将分钟至0
        calendar.set(Calendar.MINUTE, 0);
        //将秒至0
        calendar.set(Calendar.SECOND,0);
        //将毫秒至0
        calendar.set(Calendar.MILLISECOND, 0);
        // 获取本月第一天的时间戳
        //return c.getTimeInMillis() / 1000;
        return calendar.getTimeInMillis();
    }
    /**
     * 获取指定时间戳半年开始的时间戳/秒
     * @return
     */
    public static Long getSixAgoBegin() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE, 1);// 设为当前月的1号
        calendar.add(Calendar.MONTH, -5);// 0表示当前月，-2就是当前月-2
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }
    /**
     * 获取指定时间戳所在年开始的时间戳/秒
     * @param dateTimeMillis 指定时间戳/毫秒
     * @return
     */
    public static Long getYearBegin(Long dateTimeMillis, String timeZone) {
        Calendar calendar = Calendar.getInstance();// 获取当前日期
        calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
        calendar.setTimeInMillis(dateTimeMillis);
        calendar.add(Calendar.YEAR, 0);
        calendar.add(Calendar.DATE, 0);
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_YEAR, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }
    /**
     * 获取指定时间戳所在年开始的时间戳/秒
     * @param dateTimeMillis 指定时间戳/毫秒
     * @return
     */
    public static Long getYearBegin(Long dateTimeMillis) {
        Calendar calendar = Calendar.getInstance();// 获取当前日期
        calendar.setTimeInMillis(dateTimeMillis);
        calendar.add(Calendar.YEAR, 0);
        calendar.add(Calendar.DATE, 0);
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_YEAR, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }
    /**
     * 获取当年的最后时间戳
     *
     * @param dateTimeMillis 毫秒级时间戳
     * @return
     */
    public static Long getYearEnd(Long dateTimeMillis) {
        Calendar calendar = Calendar.getInstance();// 获取当前日期
        calendar.setTimeInMillis(dateTimeMillis);
        int year = calendar.get(Calendar.YEAR);
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        return calendar.getTimeInMillis();
    }
    /**
     * 获取指定时间戳所在月份开始的时间戳/秒
     * @param dateTimeMillis 指定时间戳/毫秒
     * @return
     */
    public static Long getMonthBegin(Long dateTimeMillis) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date(dateTimeMillis));

        //设置为1号,当前日期既为本月第一天
        c.set(Calendar.DAY_OF_MONTH, 1);
        //将小时至0
        c.set(Calendar.HOUR_OF_DAY, 0);
        //将分钟至0
        c.set(Calendar.MINUTE, 0);
        //将秒至0
        c.set(Calendar.SECOND,0);
        //将毫秒至0
        c.set(Calendar.MILLISECOND, 0);
        // 获取本月第一天的时间戳
        //return c.getTimeInMillis() / 1000;
        return c.getTimeInMillis();
    }

    /**
     * 获取指定时间戳所在月份15号的时间戳/秒
     * @param dateTimeMillis 指定时间戳/毫秒
     * @return
     */
    public static Long getMonthMiddle(Long dateTimeMillis) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date(dateTimeMillis));

        //设置为当月最后一天
        c.set(Calendar.DAY_OF_MONTH, 15);
        //将小时至23
        c.set(Calendar.HOUR_OF_DAY, 23);
        //将分钟至59
        c.set(Calendar.MINUTE, 59);
        //将秒至59
        c.set(Calendar.SECOND,59);
        //将毫秒至999
        c.set(Calendar.MILLISECOND, 999);
        // 获取本月最后一天的时间戳
        return c.getTimeInMillis() ;
    }

    /**
     * 获取指定时间戳所在月份结束的时间戳/秒
     * @param dateTimeMillis 指定时间戳/毫秒
     * @return
     */
    public static Long getMonthEnd(Long dateTimeMillis) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date(dateTimeMillis));

        //设置为当月最后一天
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        //将小时至23
        c.set(Calendar.HOUR_OF_DAY, 23);
        //将分钟至59
        c.set(Calendar.MINUTE, 59);
        //将秒至59
        c.set(Calendar.SECOND,59);
        //将毫秒至999
        c.set(Calendar.MILLISECOND, 999);
        // 获取本月最后一天的时间戳
        //return c.getTimeInMillis() / 1000;
        return c.getTimeInMillis();
    }

    /**
     * 获取上一个月1号0点0分0秒的时间戳
     * @param dateTimeMillis
     * @return
     */
    public static Long getPreviousMonthBegin(Long dateTimeMillis){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(dateTimeMillis));
        calendar.set(Calendar.DATE, 1);// 设为当前月的1号
        calendar.add(Calendar.MONTH, -1);// 0表示当前月，-2就是当前月-2
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();

    }

    //获取日开始时间
    public static Long getDayBegin(Long dateTimeMillis){
        Calendar c = Calendar.getInstance();
        c.setTime(new Date(dateTimeMillis));
        //将小时至00
        c.set(Calendar.HOUR_OF_DAY, 00);
        //将分钟至00
        c.set(Calendar.MINUTE, 00);
        //将秒至00
        c.set(Calendar.SECOND,00);
        //将毫秒至000
        c.set(Calendar.MILLISECOND, 000);
        // 获取本月最后一天的时间戳
        //return c.getTimeInMillis() / 1000;
        return c.getTimeInMillis();
    }
    //获取日结束时间
    public static Long getDayEnd(Long dateTimeMillis){
        Calendar c = Calendar.getInstance();
        c.setTime(new Date(dateTimeMillis));
        //将小时至23
        c.set(Calendar.HOUR_OF_DAY, 23);
        //将分钟至59
        c.set(Calendar.MINUTE, 59);
        //将秒至59
        c.set(Calendar.SECOND,59);
        //将毫秒至999
        c.set(Calendar.MILLISECOND, 999);
        // 获取本月最后一天的时间戳
        //return c.getTimeInMillis() / 1000;
        return c.getTimeInMillis();
    }

    /**
     * 获取当前日期
     * @return
     */
    public static String getCurrentDate() {
        Date date = new Date();
        return sdf.format(date);
    }
    /**
     *  日期转换时间戳
     * @return
     */
    public static Long dateToStamp(String s){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = simpleDateFormat.parse(s);
            long time = date.getTime();
            //res = String.valueOf(time);
            return time;
        } catch (ParseException e) {
            e.printStackTrace();
            //转换失败取当月1号时间戳
            return WsDateUtil.getMonthBegin(System.currentTimeMillis());
        }
    }
    /*
     * 将时间戳转换为时间
     */
    public static String stampToDate(String s){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }
    /**
     * 获取当年的最后时间戳
     *
     * @param timeStamp 毫秒级时间戳
     * @param timeZone  如 GMT+8:00
     * @return
     */
    public static Long getYearEndTime(Long timeStamp, String timeZone) {
        Calendar calendar = Calendar.getInstance();// 获取当前日期
        calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
        calendar.setTimeInMillis(timeStamp);
        int year = calendar.get(Calendar.YEAR);
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        return calendar.getTimeInMillis();
    }
    /**
     * 获取指定某一天的开始时间戳
     *
     * @param timeStamp 毫秒级时间戳
     * @return
     */
    public static Long getDailyStartTime(Long timeStamp) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone(Constant.TIME_ZONE));
        calendar.setTimeInMillis(timeStamp);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    /**
     * 获取指定某一天的结束时间戳
     *
     * @param timeStamp 毫秒级时间戳
     * @return
     */
    public static Long getDailyEndTime(Long timeStamp) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone(Constant.TIME_ZONE));
        calendar.setTimeInMillis(timeStamp);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTimeInMillis();
    }
    /**
     * @Description: 获取小时数组
     **/
    public static List<String> getHoursList() {
        List list = new ArrayList();
        for (int i = 0; i < 24; i++) {
            if(i<10){
                list.add("0"+i) ;
            }else{
                list.add(i+"");
            }
        }
        return list;
    }
    /**
     * @Description: 获取日期数组
     **/
    public static List<String> getDayOrYearList(int sum) {
        List list = new ArrayList();
        for (int i = 1; i < sum+1; i++) {
            if(i<10){
                list.add("0"+i) ;
            }else{
                list.add(i+"");
            }
        }
        return list;
    }
    /**
     * @Description: 获取某时间月份天数
     * @Param:
     **/
    public static int getDaysOfMonth(String date){
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(sdf.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
            return 30;
        }
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }
    /**
     * @Description: 获取某时间月份天数（本月已过天数）
     * @Param:
     **/
    public static int getDaysOfMonthCheck(String date){
        Calendar calendar = Calendar.getInstance();
        if(getMonthBegin(dateToStamp(date)).equals(getMonthBegin(System.currentTimeMillis()))){
            return calendar.get(Calendar.DAY_OF_MONTH);
        }
        try {
            calendar.setTime(sdf.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
            return 30;
        }
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }
    /**
     * @Description: 获取前一天
     * @Param: [time]
     **/
    public static Long getLastDay(String time){
        Calendar calendar = Calendar.getInstance();
        Date date=null;
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        calendar.setTime(date);
        int day=calendar.get(Calendar.DATE);
        //                      此处修改为+1则是获取后一天
        calendar.set(Calendar.DATE,day-1);
        String lastDay = sdf.format(calendar.getTime())+" 00:00:00";
        return  WsDateUtil.dateToStamp(lastDay);
    }
    /**
     * @Description: 获取前一月
     * @Param: [time]
     **/
    public static Long getLastMonth(String time){
        Calendar calendar = Calendar.getInstance();
        Date date=null;
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        calendar.setTime(date);
        int month=calendar.get(Calendar.MONTH);
        //                      此处修改为+1则是获取后一天
        calendar.set(Calendar.MONTH,month-1);
        String lastMonth = sdf.format(calendar.getTime())+" 00:00:00";
        //System.out.println("lastDay:"+lastMonth);
        return  WsDateUtil.dateToStamp(lastMonth);
    }
    /**
     * @Description: 获取前一年
     * @Param: [time]
     **/
    public static Long getLastYear(String time){
        Calendar calendar = Calendar.getInstance();
        Date date=null;
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        calendar.setTime(date);
        int year=calendar.get(Calendar.YEAR);
        //                      此处修改为+1则是获取后一天
        calendar.set(Calendar.YEAR,year-1);
        String lastYear = sdf.format(calendar.getTime())+" 00:00:00";
        //System.out.println("lastYear:"+lastYear);
        return  WsDateUtil.dateToStamp(lastYear);
    }
}
